<?php

namespace app\common\model\goods;

use think\Model;
use think\Db;

class GoodsHouseStock extends Model
{

    

    

    // 表名
    protected $name = 'goods_house_stock';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];
    public function getstock($goods_id,$storehouse_id =null){
        if(empty($storehouse_id)){
            $where = array('goods_id'=>$goods_id);
            $stock = Db::name('goods_house_stock')->where($where)->sum('stock');
            return  $stock;
        }else{
            $where = array(
                        'storehouse_id'=>$storehouse_id,
                        'goods_id'=>$goods_id
                     );
        }
        $row = Db::name('goods_house_stock')->where($where)->find();
        $stock = empty($row['stock'])?0:$row['stock'];
        return $stock;
    }
    //减少
    public function reducestock($goods_id,$storehouse_id,$num,$price=null){
//        if($goods_id == 1){
//            return true;
//        }
        $sinfo = Db::name('goods_house_stock')->where(array('goods_id'=>$goods_id,'storehouse_id'=>$storehouse_id))->find();

        if($sinfo){
            if($sinfo['stock'] <$num){
                return false;
            }
            if(empty($price)){
                //库存为0 就清空余额
                if($sinfo['stock']-$num == 0){
                    $balance = 0;
                }else{
                    $newbalance = ($sinfo['balance']/$sinfo['stock'])*$num;
                    $balance = $sinfo['balance'] - $newbalance;
                }
                $res = Db::name('goods_house_stock')
                    ->where(array('id'=>$sinfo['id']))
                    ->update(array(
                        'stock'=>$sinfo['stock']-$num,
                        'balance'=>$balance,
                    ));
            }else{
                //库存为0 就清空余额
                if($sinfo['stock']-$num == 0){
                    $weighting_cost =0;
                    $balance = 0;
                }else{
                    $balance = $sinfo['balance'] - $num*$price;
                    $weighting_cost = $balance/($sinfo['stock']-$num);
                }
                $res = Db::name('goods_house_stock')
                    ->where(array('id'=>$sinfo['id']))
                    ->update(array(
                        'stock'=>$sinfo['stock']-$num,
                        'weighting_cost'=>$weighting_cost,
                        'balance'=>$balance,
                    ));
            }
//            $is_sync = config()
            //同步库存
            $is_sync = config('site.is_sync');
            if($is_sync){
                $row = Db::name('goods')->where(array('id'=>$goods_id))->find();
                if($row['sync_status'] == 1) {//只有设置了及时更新 才会更新
                    $hnuo = Db::name('storehouse')->where(array('id' => $storehouse_id))->find();
                    $url = config('site.syncurl') . '/api/sync/syncstock';
                    curl_post($url, array(
                        'spec_num' => $row['number'],
                        'code' => $hnuo['code'],
                        'stock' => $sinfo['stock'] - $num,
                        'appkey' => config('site.appkey'),
                    ));
                }
            }
            if($res){
                return true;
            }
        }
        return false;
    }
    //增加
    public function addstock($goods_id,$storehouse_id,$num,$price = null,$is_add = 0){
        $sinfo = Db::name('goods_house_stock')->where(array('goods_id'=>$goods_id,'storehouse_id'=>$storehouse_id))->find();
        if(!$sinfo){
            $ree= Db::name('goods_house_stock')->insertGetId(array(
                'goods_id'=>$goods_id,
                'storehouse_id'=>$storehouse_id,
                'stock'=>0,
            ));
            if(!$ree){
                return  false;
            }
            $sinfo['id'] = $ree;
            $sinfo['stock'] = 0;
            $sinfo['balance'] = 0;
            $sinfo['cumulative'] = 0;
        }

        if(empty($price)){ //在原基础上加

            $addbalance = empty($sinfo['stock'])?0:($sinfo['balance']/$sinfo['stock'])*$num;
            $res = Db::name('goods_house_stock')
                ->where(array('id'=>$sinfo['id']))
                ->update(array(
                    'cumulative'=>$sinfo['cumulative']+$num,
                    'stock'=>$sinfo['stock']+$num,
                    'balance'=>$sinfo['balance']+$addbalance,
                ));
        }else{

            $balance = $sinfo['balance']+$price*$num;
            $weighting_cost = $balance/($num+$sinfo['stock']);
            if($is_add == 1){
                $cumulative = $sinfo['cumulative'];
            }else{
                $cumulative = $sinfo['cumulative']+$num;
            }
            $res = Db::name('goods_house_stock')
                ->where(array('id'=>$sinfo['id']))
                ->update(array(
                    'cumulative'=>$cumulative,
                    'stock'=>$sinfo['stock']+$num,
                    'weighting_cost'=>$weighting_cost,
                    'balance'=>$balance,
                    'new_cost'=>$price,
                ));
        }
        $is_sync = config('site.is_sync');
        if($is_sync){
            $row = Db::name('goods')->where(array('id'=>$goods_id))->find();
            if($row['sync_status'] == 1){//只有设置了及时更新 才会更新
                $hnuo = Db::name('storehouse')->where(array('id'=>$storehouse_id))->find();

                $url = config('site.syncurl').'/api/sync/syncstock';
                curl_post($url,array(
                    'spec_num'=>$row['number'],
                    'code'=>$hnuo['code'],
                    'stock'=>$sinfo['stock']+$num,
                    'appkey'=>config('site.appkey'),
                ));
            }
        }
        if($res){
            return true;
        }
        return false;
    }
    //获取虚拟库存
    public function getfictstock($goods_id,$storehouse_id){
        $stock = $this->getstock($goods_id,$storehouse_id);
        //获取销售单已开数量
        $xcount = Db::name('sales_goods')
            ->alias('g')
            ->join('sales_slip s','g.sales_slip_id = s.id')
//            ->where(array('g.status'=>array('in',[1,2]),'g.storehouse_id'=>$storehouse_id,'goods_id'=>$goods_id,'s.status'=>array('<',6)))
            ->where(array('g.status'=>array('in',[1,2]),'g.storehouse_id'=>$storehouse_id,'goods_id'=>$goods_id,'s.status'=>array('<',6)))
            ->sum('goods_num');
        //获取采购单订购数量
        $ccount = Db::name('purchase_goods')
            ->alias('g')
            ->join('purchase_order o','g.purchase_order_id = o.id')
            ->where(array('o.status'=>array('in',[1,2]),'g.storehouse_id'=>$storehouse_id,'goods_id'=>$goods_id))
            ->sum('goods_num');

        return $stock-$xcount+$ccount;
    }

}
